//app.js
//userName是表中人名，UserName登录用户名
App({
  onLaunch: function() {
    // 展示本地存储能力
    /*var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('app.js launch logs', logs)*/
    //获取用户名密码本地缓存
    let name = null;
    let pwd = null;
    let open = null;
    name = wx.getStorageSync('name')
    pwd = wx.getStorageSync('pwd')    
    open = wx.getStorageSync('open')

    let that= this;
    //this.noStorageToLog(name, pwd, open);
    wx.getSetting({    //index中重复已经注释掉了
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log("app.js 已有授权 getSetting userInfo", res, res.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          });
          this.noStorageToLog(name, pwd, open);//that
        }
        else {  //尚未授权
          wx.reLaunch({   //关闭所有页面并跳转
            url: '/pages/auth/auth'
          })
          console.log("app getSetting no authorization")
        }
      }
    });    
    
    console.log("app get from storage", name, pwd,open)
    this.globalData.UserName = name; //这就是全局的用户名了
    // 获取用户信息
   /* wx.getSetting({    //index中重复
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              console.log("app.js getSetting userInfo",res,res.userInfo)
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
             // wx.redirectTo({
               // url: '/pages/index/index',
             // });
            }
          })
        }
      }
    })*/
    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.CustomBar = 64;
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
        console.log("app sysinfo", e)
        console.log("app sysinfo", e.statusBarHeight)
        console.log("cus",custom)
        console.log("cusbu", custom.bottom + custom.top)
        wx.setStorageSync("test", custom.bottom)
        var value = wx.getStorageSync("test")
        console.log(value);
      }
    });
    this.getPeople();  //获取全局上传人
    console.log("app.js out people",this.globalData.People)
    setTimeout(() => { console.log("pe", this.globalData.People)},800);
    
  },
  globalData: {
    userInfo: null,
    People:[],  
  },
  getPeople:function(){  //get 人员
    let people=[];
    let that=this;
    wx.request({
      url: 'http://10.10.11.173:8989/getUser',  //''传空值查全部
      header: {
        "Content-Type": "application/json"
      },
      method: 'get',
      data:{KEY:''},
      success: function (res) {
        console.log("app.js people suc", res);
        people=res.data;
        console.log("app.js people peo", people);
        let names=[]
        people.forEach(function(value){
          names.push(value.NAME);
        });
        console.log(names);        
        that.globalData.People=people;
        console.log("gp", that.globalData.People);
      },
      fail: function (res) {
        console.log("fail", res);
      },
    });
  },
  
  //放入getsettin授权信息中 index的去掉应该 这样 auth可以判断是否授权而少点一次按钮
  noStorageToLog: function (name,pwd,open) {
    if (!name || !pwd || !open) {  //缓存不存在就去注册授权
      console.log("app noStorageToLog 缓存不存在",name,pwd,open)
      /*wx.navigateTo({
        url: '/pages/auth/auth',
      })*/
      wx.reLaunch({
        url: '/pages/auth/auth'
      })       
    }
    else {      //缓存存在 用普通request缓存发送请求换取user信息
      // 登录  
      console.log("app noStorageToLog 缓存存在", name, pwd, open)
      let that=this;
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log("app.js login", res)
          let code = res.code   //发给后台
          wx.request({   //可能是直接用openid换取user信息就行了
            url: 'http://10.10.11.195:8080/szgd/weChat/getOpenId?name=' + name + "&pass=" + pwd + "&code=" + code, //获取openID和用户对象
            //data: { name: name, pass: pwd ,code:code},
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {  //返回user对象
              console.log("app send code ", res)
              let userName = res.data.name;
              let userWork = res.data.workType;
              let userId = res.data.id;
              let openid = res.data.openId;
              that.globalData.userName = userName;
              that.globalData.userWork = userWork;
              that.globalData.userId = userId;
              that.globalData.openid = openid;
              wx.setStorageSync('open', openid);
              console.log("app get from code", that.globalData.userName, that.globalData.userWork, that.globalData.userId, that.globalData.openid);
              //职位回调，交给index，确保app onload 职位放入了全局后inde里面能拿到
              if (that.userWorkCallback) {
                console.log("app work callback")
                that.userWorkCallback(res.data.workType)
              }
            }
          })          
        }//login suc
      })
    }
  },

})


/*let appid ='wx21890e2fe08b62cb';  服务器调用
          let secret ='1cc68a1cc05488a0dd823f23130f994d'
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&js_code=' + code + '&grant_type=authorization_code',
            data: {},
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log('app login req res', res)
              let openid = res.data.openid //返回openid
              console.log('app login req openid',openid)
            }
          })*/